const swiperModel = require('../model/swiperModel.js');
const carModel = require("../model/carModel");

const swiperController = {
    //获取轮播图列表
    GetSwiperList: async(req,res)=>{
        try {
            const result = await swiperModel.GetSwiperList(req,res);
            res.send({
                code: 1,
                msg: "成功获取轮播图列表",
                list: result,
            });
        } catch (error) {
            console.log(error,"获取轮播图列表出现error");
        };
    },
    //添加轮播图
    AddSwiper: async (req,res)=>{
        try {
            const result = await swiperModel.AddSwiper(req.body,req.file);
            if(result){
                res.send({
                    code: 1,
                    msg: "添加轮播图成功",
                });
            }else{
                res.send({
                    code: 0,
                    msg: "添加轮播图失败,请填写正确的参数",
                    status:401
                });
            }
        } catch (error) {
            console.log(error,"添加轮播图出现error");
        };
    },
    //更新轮播图
    UpdateSwiper: async (req,res)=>{
        try {
            const result = await swiperModel.UpdateSwiper(req.body,req.file);
            if(result){
                res.send({
                    code:1,
                    msg:"更新轮播图成功",
                });
            }else{
                res.send({
                    code:0,
                    msg:"更新轮播图失败",
                });
            }
        } catch (error) {
            console.log(error,'更新公告出现error');
        }
    },
    //删除轮播图
    DeleteSwiper: async (req,res)=>{
        try {
            const result = await swiperModel.DeleteSwiper(req.body);
            if(result){
                res.send({
                    code: 1,
                    msg: "删除轮播图成功",
                });
            }else{
                res.send({
                    code: 0,
                    msg: "删除轮播图失败",
                });
            }
        } catch (error) {
            console.log(error,'删除轮播图出现error');
        };
    },

    BatchDeleteSwiper: async (req,res)=>{
        try {
            let result = await swiperModel.BatchDeleteSwiper(req.body);
            if(result.affectedRows !== 0){
                res.status(200).send({
                    code:1,
                    msg:"批量删除轮播图成功",
                });
            }else{
                res.status(404).send({
                    code:0,
                    msg:"批量删除轮播图失败",
                });
            }
        }catch (error){
            console.log(error,'controller层批量删除轮播图出现error');
        }
    },
    //获取后台管理轮播图
    GetAdminSwiper: async (req,res)=>{

        try {
            const result = await swiperModel.GetAdminSwiper();
            console.log(result,'后台轮播图结果');
            if(result){
                res.send({
                    code: 1,
                    msg: "获取后台管理轮播图成功",
                    list:result
                });
            }else{
                res.send({
                    code: 0,
                    msg: "获取后台管理轮播图失败",
                });
            }
        } catch (error) {
            console.log(error,'获取后台管理轮播图出现error');
        };
    },
    //获取客户端系统轮播图
    GetClientSwiper: async (req,res) =>{
        try {
            const result = await swiperModel.GetClientSwiper();
            console.log(result,'客户端轮播图结果');
            if(result){
                res.send({
                    code: 1,
                    msg: "获取客户端轮播图成功",
                    list:result
                });
            }else{
                res.send({
                    code: 0,
                    msg: "获取客户端轮播图失败",
                });
            }
        } catch (error) {
            console.log(error,'获取客户端轮播图出现error');
        };
    }
};

module.exports = swiperController;